/*
 * (c) Copyright 2001 MyCorporation.
 * All Rights Reserved.
 */
package net.sourceforge.p4eclipse.core.api;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import net.sourceforge.p4eclipse.core.util.Form;
import net.sourceforge.p4eclipse.core.util.Form.BasicFieldParser;

/**
 * Parses and represents a Perforce client-workspace
 * 
 * View is not exposed, and not parsed yet.
 * @version 	1.0
 * @author     <a href="mailto:dcorbin@users.sourceforge.net">David Corbin</a>
 */
public class ClientForm extends Form
{
	public static class OptionFieldParser extends BasicFieldParser
	{
		private void setOption(Map map, String word)
		{
			word = word.trim();
			Boolean b = Boolean.TRUE;
			if (word.startsWith("no") || word.startsWith("un"))
			{
				b = Boolean.FALSE;
				word = word.substring(2);
			}
			map.put(word,b);
		}
		public Object parse() 
		{
			Map map = new HashMap();
			String input = buffer.toString();
			while (input.length() > 0)
			{
				int spacePosition = input.indexOf(' ');
				if (spacePosition > 0)
				{
					String word = input.substring(0,spacePosition);
					input = input.substring(spacePosition+1);
					setOption(map,word);
				}
				else
				{
					setOption(map,input);
					break;
				}
				
			}	
			return map;		
		}
	}
	/**
	 * Constructor for ClientFOrm.
	 * @param inputReader
	 * @throws IOException
	 */
	public ClientForm()
	{
		super();
		registerFieldParser("Options", new OptionFieldParser());
	}
	private Boolean getOption(String optionName)
	{
		Map map = (Map)getField("Options");
		return (Boolean)map.get(optionName);
	}
	
	public Boolean getClobber()		{ return getOption("clobber"); }
	public Boolean getCompress() 	{ return getOption("compress"); }
	public Boolean getRmDir() 		{ return getOption("rmdir"); }
	public Boolean getLock() 		{ return getOption("lock"); }
	public Boolean getModTime()		{ return getOption("modtime");	}
	public Boolean getAllWrite() 	{ return getOption("allwrite");	}
	public String getRoot() 		{ return (String)getField("Root"); }
	public String getClient() 		{ return (String)getField("Client"); }
	public String getUpdate() 		{ return (String)getField("Update"); }
	public String getAccess() 		{ return (String)getField("Access"); }
	public String getOwner() 		{ return (String)getField("Owner"); }
	public String getHost() 		{ return (String)getField("Host"); }
	public String getLineEnd() 		{ return (String)getField("LineEnd"); }

	public void setRoot(String value) { setField("Root",value); }
}
